package com.cskaoyan.service.wx;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponMylistResult;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.controller.wx.CouponRespVo;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author WeitenHong
 * @date 2020/10/8 11:11
 */
@Service
public class WxCouponServiceImpl implements WxCouponService {
    @Autowired
    CouponUserMapper couponUserMapper;

    @Autowired
    CouponMapper couponMapper;

    @Override
    public HashMap<String, Object> showMyCouponList(Integer status, Integer page, Integer size, Integer userId) {

        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(status.shortValue()).andDeletedEqualTo(false);
        List<CouponUser> couponusers = couponUserMapper.selectByExample(couponUserExample);

        List<Integer> couponIdList = new ArrayList<>();
        for (CouponUser c : couponusers
        ) {
            couponIdList.add(c.getCouponId());
        }


        List<CouponMylistResult> coupons = new ArrayList();
        for (Integer i : couponIdList) {
            CouponMylistResult couponMylistResult = couponMapper.selectById(i);
            CouponUser couponUser = couponUserMapper.selectCouponUserByCouponId(i,userId);
            couponMylistResult.setStartTime(couponUser.getStartTime());
            couponMylistResult.setEndTime(couponUser.getEndTime());
            coupons.add(couponMylistResult);
        }
        PageHelper.startPage(page, size);
        HashMap<String, Object> bigMap = new HashMap();
        PageInfo<CouponMylistResult> couponPageInfo = new PageInfo(coupons);
        bigMap.put("count", couponPageInfo.getTotal());
        bigMap.put("data", coupons);
        return bigMap;
    }

    @Override
    public List<CouponMylistResult> selectCoupon(Integer cartId, Integer grouponRulesId) {
        Integer userId = IdUtils.getId();
        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.createCriteria().andStatusEqualTo((short) 0).andUserIdEqualTo(userId);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);



        List<CouponMylistResult> list=new ArrayList();
        for (CouponUser c : couponUsers) {
            Integer couponId = c.getCouponId();
            CouponMylistResult couponMylistResult = couponMapper.selectById(couponId);
            couponMylistResult.setStartTime(c.getStartTime());
            couponMylistResult.setEndTime(c.getEndTime());
            list.add(couponMylistResult);
        }
        return list;
    }

    @Override
    public CouponRespVo exchangeCouponByCode(String code, Integer userId) {
        CouponRespVo couponRespVo = new CouponRespVo();

        Integer Couponid = couponMapper.selectIdByCode(code);
        if (Couponid != null) {
            //通过优惠券id,到coupon表中查出该优惠券对应的数据，封装至Coupon
            Coupon coupon = couponMapper.selectByPrimaryKey(Couponid);
//        Integer Couponid=coupon.getId();
            //判断优惠券数目
            CouponUserExample couponUserExample = new CouponUserExample();
            couponUserExample.createCriteria().andCouponIdEqualTo(Couponid).andUserIdEqualTo(userId);
            List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
            if (couponUsers.size()==0) {
                //如果这个优惠券不在couponuser中，准备将数据插入couponuser
                CouponUser couponUser = new CouponUser();
                couponUser.setCouponId(Couponid);
                couponUser.setUserId(userId);
                Date date = new Date(System.currentTimeMillis());
                couponUser.setStartTime(coupon.getStartTime());
                couponUser.setAddTime(date);
                couponUser.setEndTime(coupon.getEndTime());
                couponUser.setUpdateTime(date);
                couponUserMapper.insertSelective(couponUser);//记得insert = =
                //更新优惠券数目到coupon表
                if (coupon.getTotal() > 0) {
                    coupon.setTotal(coupon.getTotal() - 1);
                    couponMapper.updateByPrimaryKey(coupon);
                }
                couponRespVo.setErrmsg("成功");
                couponRespVo.setErrno(0);
            } else {
                couponRespVo.setErrmsg("优惠券已兑换");
                couponRespVo.setErrno(740);
            }
        }
        if (Couponid == null) {
            couponRespVo.setErrmsg("优惠券不正确");
            couponRespVo.setErrno(742);
        }
        return couponRespVo;
    }
}
